package com.ruoyi.binance.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.binance.domain.Binance15minK;
import com.ruoyi.binance.mapper.Binance15minKMapper;
import com.ruoyi.binance.service.IBinance15minKService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 15分钟实时K线数据Service业务层处理
 *
 * @author zq
 * @date 2025-10-17
 */
@Service
public class Binance15minKServiceImpl implements IBinance15minKService {
    @Autowired
    private Binance15minKMapper binance15minKMapper;

    /**
     * 查询15分钟实时K线数据
     *
     * @param code 15分钟实时K线数据主键
     * @return 15分钟实时K线数据
     */
    @Override
    public Binance15minK selectBinance15minKByCode(String code) {
        return binance15minKMapper.selectBinance15minKByCode(code);
    }

    /**
     * 查询15分钟实时K线数据列表
     *
     * @param binance15minK 15分钟实时K线数据
     * @return 15分钟实时K线数据
     */
    @Override
    public List<Binance15minK> selectBinance15minKList(Binance15minK binance15minK) {
        return binance15minKMapper.selectBinance15minKList(binance15minK);
    }

    /**
     * 新增15分钟实时K线数据
     *
     * @param binance15minK 15分钟实时K线数据
     * @return 结果
     */
    @Override
    public int insertBinance15minK(Binance15minK binance15minK) {
        binance15minK.setCreateTime(DateUtils.getNowDate());
        return binance15minKMapper.insertBinance15minK(binance15minK);
    }

    /**
     * 修改15分钟实时K线数据
     *
     * @param binance15minK 15分钟实时K线数据
     * @return 结果
     */
    @Override
    public int updateBinance15minK(Binance15minK binance15minK) {
        binance15minK.setUpdateTime(DateUtils.getNowDate());
        return binance15minKMapper.updateBinance15minK(binance15minK);
    }

    /**
     * 批量删除15分钟实时K线数据
     *
     * @param codes 需要删除的15分钟实时K线数据主键
     * @return 结果
     */
    @Override
    public int deleteBinance15minKByCodes(String[] codes) {
        return binance15minKMapper.deleteBinance15minKByCodes(codes);
    }

    /**
     * 删除15分钟实时K线数据信息
     *
     * @param code 15分钟实时K线数据主键
     * @return 结果
     */
    @Override
    public int deleteBinance15minKByCode(String code) {
        return binance15minKMapper.deleteBinance15minKByCode(code);
    }

    @Override
    public void saveOrUpdateMinK(String code, JSONArray jsonArray) {
        if (jsonArray == null || jsonArray.isEmpty()) {
            return;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            Binance15minK minK = new Binance15minK();
            minK.setCode(code);
            minK.setTime(array.getDate(0));
            minK.setOpen(array.getBigDecimal(1));
            minK.setHigh(array.getBigDecimal(2));
            minK.setLow(array.getBigDecimal(3));
            minK.setClose(array.getBigDecimal(4));
            minK.setVolume(array.getBigDecimal(5));
            minK.setCloseTime(array.getDate(6));
            minK.setAmount(array.getBigDecimal(7));
            minK.setNumber(array.getLong(8));
            int updated = updateBinance15minK(minK);
            if (updated == 0) {
                insertBinance15minK(minK);
            }
        }
    }
}
